class CreatequizController < ApplicationController
  def newquiz
    @quiz = Quiz.new
    @title = params[:title]
    @description = params[:description]
    @scoring = params[:scoring]
    @isquiz = params[:isquiz]
    @fail= params[:fail]
  end
  
  def newquestion
    @question = Question.new
    @quiz = Quiz.find(session[:quiz_id])
    @title = params[:title]
    @body = params[:body]
    @anstype = params[:anstype]
    @fail= params[:fail]
  end

  def addanswer
    @question = Question.find(params[:questionid])
    @body = params[:body]
    @score= params[:score]
    @fail= params[:fail]
    i = 0
    @question.answers.order("spot ASC").each do |a|
      i = i + 1
      a.spot = i
      a.save
    end
  end

  def createanswer
    @question = Question.find(params[:questionid])
    i = 0
    @question.answers.order("spot ASC").each do |a|
      i = i + 1
      a.spot = i
      a.save
    end
    ans = @question.answers.create(params[:answer])
    ans.question = @question
    ans.anstype = @question.anstype
    ans.spot = @question.answers.length + 1
    if ans.score == ""
      ans.score = 0
    end
    if ans.save
      redirect_to :action => :addanswer, :questionid => @question.id
    else
      redirect_to :action => :addanswer, :questionid => @question.id, :fail => ans.errors, :body =>ans.body, :score => ans.score
    end
    
  end

  def createquiz
    @user = User.find(session[:user_id])
    @quiz = @user.quizzes.create
    @quiz.title = params[:title]
    @quiz.description = params[:description]
    @quiz.scoring = params[:scoring]
    @quiz.isquiz = ! params[:survey]


    respond_to do |format|
      if @quiz.save
        format.html { redirect_to(:action => :editquiz, :quiz_id => @quiz.id) }
      else
        format.html { redirect_to(:action => :newquiz, :fail => @quiz.errors, :title => params[:title], :description =>params[:description],:scoring =>params[:scoring], :isquiz => params[:survey]) }
      end
    end

  end
  def createquestion
    @quiz = Quiz.find(session[:quiz_id])
    i = 0
    @quiz.questions.order("spot ASC").each do |q|
      i = i + 1
      q.spot = i
      q.save
    end
    @question = @quiz.questions.create
    @question.title = params[:title]
    @question.body = params[:body]
    @question.anstype = params[:anstype]
    @question.spot = @quiz.questions.length + 1

    if @question.save
      if session[:add_just_one]
        session[:question_id] = @question.id
        redirect_to(:action => :editquiz, :quiz_id => @quiz.id )
      else
        redirect_to(:action => :newquestion)
      end
    else
      redirect_to(:action => :newquestion, :fail =>@question.errors, :title => params[:title], :body => params[:body],:anstype => params[:anstype])
    end
  end


  def index
    @quizzes = Quiz.all
  end

  def editquiz
    @quiz = Quiz.find(params[:quiz_id])
    @fail = params[:fail]
    i = 0
    @quiz.questions.order("spot ASC").each do |q|
      i = i + 1
      q.spot = i
      q.save
    end
    session[:quiz_id] = @quiz.id
    session[:add_just_one] = true
  end

  def editquizinfo
    @quiz = Quiz.find(params[:quiz_id])
    @fail = params[:fail]
    session[:quiz_id] = @quiz.id
  end

  def quizready
    @quiz = Quiz.find(params[:quiz_id])
    fail = ""
    if ! @quiz.ready
      if @quiz.questions == []
        fail = fail + "there are no questions"
      else
        @quiz.questions.each do |q|
          if @quiz.isquiz
            if q.anstype == "Free"
              fail = fail + "a free answer question cannot be in a regular quiz, "
            end
          end
          if q.answers == [] && q.anstype != "Free"
            fail = fail + "a Question needs answers, "
          end
        end
      end

      if fail == ""
        @quiz.ready = ! @quiz.ready
        @quiz.save

        redirect_to(:action => :editquiz, :quiz_id =>@quiz.id)
      else
        fail = "because "+fail
        redirect_to(:action => :editquiz, :quiz_id =>@quiz.id, :fail => fail)
      end
      
    else
      @quiz.ready = ! @quiz.ready
      @quiz.save

      redirect_to(:action => :editquiz, :quiz_id =>@quiz.id)
    end



  
  end


  def savequizinfo
    @quiz = Quiz.find(params[:quiz_id])
    @quiz.title = params[:title]
    @quiz.description = params[:description]
    @quiz.scoring = params[:scoring]
    @quiz.isquiz = ! params[:survey]

    respond_to do |format|
      if @quiz.save
        format.html {redirect_to(:action => :editquiz, :quiz_id =>@quiz.id)}
      else
        format.html {redirect_to(:action => :editquizinfo, :fail => @quiz.errors, :quiz_id => @quiz.id) }
      end
    end
    
  end
  
  def editquestion
    @question = Question.find(params[:questionid])
    @quiz = Quiz.find(session[:quiz_id])
    @fail = params[:fail]
  end
  def saveeditquestion
    @question = Question.find(params[:questionid])
    @question.title = params[:title]
    @question.body = params[:body]
    @question.anstype = params[:anstype]

    respond_to do |format|
      if @question.save
        @question.answers.each do |a|
          if @question.anstype == "Free"
          a.destroy
          else
          a.anstype = @question.anstype
          a.save
          end
        
        end
        format.html {redirect_to(:action => :editquiz, :quiz_id => @question.quiz)}
      else
        format.html {redirect_to(:action => :editquestion, :fail => @question.errors, :questionid => @question.id) }
      end
    end
  end

  def moveupq
    @quiz = Quiz.find(params[:quiz_id])
    spot = params[:spot].to_i
    up = params[:up]
    if up
      if spot != 1
        q1 = @quiz.questions.order("spot ASC")[spot - 1]
        q2 = @quiz.questions.order("spot ASC")[spot - 2]
        temp = q1.spot
        q1.spot = q2.spot
        q2.spot = temp
        q1.save
        q2.save
      end
    else
      if spot != @quiz.questions.length
        q1 = @quiz.questions.order("spot ASC")[spot - 1]
        q2 = @quiz.questions.order("spot ASC")[spot]
        temp = q1.spot
        q1.spot = q2.spot
        q2.spot = temp
        q1.save
        q2.save
      end
    end

    redirect_to(:action => :editquiz, :quiz_id => @quiz)
  end

  def moveupanswer
    @question = Question.find(params[:questionid])
    spot = params[:spot].to_i
    up = params[:up]
    if up
      if spot != 1
        q1 = @question.answers.order("spot ASC")[spot - 1]
        q2 = @question.answers.order("spot ASC")[spot - 2]
        temp = q1.spot
        q1.spot = q2.spot
        q2.spot = temp
        q1.save
        q2.save
      end
    else
      if spot != @question.answers.length
        q1 = @question.answers.order("spot ASC")[spot - 1]
        q2 = @question.answers.order("spot ASC")[spot]
        temp = q1.spot
        q1.spot = q2.spot
        q2.spot = temp
        q1.save
        q2.save
      end
    end
    redirect_to :action => :addanswer, :questionid => params[:questionid]
  end

  def deleteq
    q = Question.find(params[:id])
    if q
      q.answers.each do |a|
        a.destroy
      end
      q.destroy
    end
    redirect_to :action => :editquiz, :quiz_id => params[:quiz_id]
  end

  def deleteanswer
    a = Answer.find(params[:id])
    if a
      a.destroy
    end
    redirect_to :action => :addanswer, :questionid => params[:questionid]
  end

  def deletequiz
    quiz = Quiz.find(params[:quiz_id])
    if quiz
      quiz.questions.each do |q|
        q.answers.each do |a|
          a.destroy
        end
        q.destroy
      end
      quiz.destroy
    end
    redirect_to :action => :index
  end

end
